package com.wxb.hello.activemq.consumer;

import com.wxb.hello.activemq.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.support.JmsUtils;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;

/**
 * describe:
 *
 * @author : 王校兵
 * @version : v1.0
 * @time : 2017/7/29  21:59
 */

public class EmailConsumer implements javax.jms.MessageListener {

    @Autowired
    private EmailService service;

    @Override
    public void onMessage(Message message) {
        if (message instanceof MapMessage) {

            MapMessage mapMessage = (MapMessage) message;
            try {
                String to = mapMessage.getString("to");
                String subject = mapMessage.getString("subject");
                String content = mapMessage.getString("content");

                service.sendSimpleMail(to, subject, content);
//                throw new JMSException("process failed");
            } catch (JMSException e) {
                e.printStackTrace();
                throw JmsUtils.convertJmsAccessException(e);
            }
        }
    }
}
